package com.ms.p2p.utils.impl;

import com.ms.common.utils.MsDateUtils;
import com.ms.p2p.bean.Stay;
import com.ms.p2p.entity.loan.Loan;
import com.ms.p2p.utils.AverageCapitalPlusInterestUtils;

import java.math.BigDecimal;

/**用来计算没一期还款金额*/
public class StayUtil {


    public static Stay getMoney(Loan loan, int i){
        Stay stay = new Stay();
        if(loan.getPay_style().equals("1")){
            BigDecimal account = AverageCapitalPlusInterestUtils.getPerMonthPrincipalInterest(loan.getAccount().doubleValue(),loan.getApr().doubleValue()/100,loan.getTime_limit());
            BigDecimal capital = AverageCapitalPlusInterestUtils.getPerMonthPrincipal(loan.getAccount().doubleValue(),loan.getApr().doubleValue()/100,loan.getTime_limit()).get(i+1);
            BigDecimal interest = account.subtract(capital);
            stay.time = MsDateUtils.getMonthTime(i + 1);
            stay.stay_account = account;
            stay.stay_capital =capital;
            stay.stay_interest =interest;
        }
        if(loan.getPay_style().equals("0")){
            BigDecimal interest = new BigDecimal(0);
            if (loan.getIs_day() == 1) {
                stay.time = MsDateUtils.getDayTime(loan.getTime_limit());

                stay.stay_interest = loan.getAccount().multiply(loan.getApr().divide(new BigDecimal(36000), 10, 4))
                        .multiply(BigDecimal.valueOf(loan.getTime_limit())).setScale(2, 4);
            } else {
                stay.time  = (MsDateUtils.getMonthTime(loan.getTime_limit()));
                stay.stay_interest = loan.getAccount().multiply(loan.getApr().divide(new BigDecimal(1200), 10, 4))
                        .multiply(BigDecimal.valueOf(loan.getTime_limit())).setScale(2, 4);
            }
            stay.stay_account = stay.stay_interest.add(loan.getAccount());
            stay.stay_capital = loan.getAccount();


        }
        if(loan.getPay_style().equals("5")){

            BigDecimal interest = loan.getAccount().multiply(loan.getApr().divide(new BigDecimal(1200), 10, 4)).setScale(2, 4);// 月利息
            if (i == 0) {//第一期
                stay.time  =(MsDateUtils.getDayTimeRepay(i));
            } else {
                stay.time   = (MsDateUtils.getMonthTime(i));
            }
            stay.stay_capital = (loan.getAccount().divide(new BigDecimal(loan.getTime_limit()), 10, 4).setScale(2, BigDecimal.ROUND_DOWN));
            BigDecimal interest1 = loan.getAccount().multiply(loan.getApr().divide(new BigDecimal(1200), 10, 4))
                    .multiply(BigDecimal.valueOf(loan.getTime_limit())).setScale(2, 4).divide(new BigDecimal(loan.getTime_limit()), 10, 4).setScale(2, BigDecimal.ROUND_DOWN);
            stay.stay_interest=interest1;
            stay.stay_account = (interest.add(stay.stay_capital).setScale(2, BigDecimal.ROUND_DOWN));

        }
        if(loan.getPay_style().equals("3")){
            BigDecimal interest = loan.getAccount().multiply(loan.getApr().divide(new BigDecimal(1200), 10, 4)).setScale(2, 4);// 月利息
            stay.time = (MsDateUtils.getMonthTime(i + 1));
            stay.stay_interest = (interest);//前面还利息
            if (i == loan.getTime_limit() - 1) {// 最后一期还本金
                stay.stay_capital = (loan.getAccount());// 本金
                stay.stay_account  = (interest.add(loan.getAccount()));// 最后一期还款总额
            } else {
                stay.stay_capital = (new BigDecimal(0));
                stay.stay_account  = (interest);// 最后一期还款总额
            }

        }
        return stay;

    }
}
